package com.disktalk.dao;

import com.disktalk.entry.DiskFile;
import com.disktalk.util.StringUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class DiskFileDao {
	@Resource
	private JdbcTemplate jdbcTemplate;

	private static List<DiskFile> diskFileList;

	private static DiskFile mDiskFile;

	/**
	 * 添加文件
	 *
	 * @param diskFile
	 */
	public void addDiskFile(DiskFile diskFile) {
		String sql = "insert into diskFile(UUID,fileName,fileSize,fileExt,fileUrl,fileFlag,dfsGroupName,uploadDate,uploadUser,expirationDate,ispublic,GroupName,downloadNum,downloadPwd,extra1,extra2,extra3,extra4,extra5) "
				+ "values (?,?,?,?,?,?,?,NOW(),?,DATE_SUB(NOW(),interval -7 day),?,?,0,?,0,0,0,0,0)";
		Object[] params = { diskFile.getUuid(), diskFile.getFileName(), diskFile.getFileSize(),
				diskFile.getFileExt(), diskFile.getFileUrl(), diskFile.getFileFlag(),
				diskFile.getDfsGroupName(), diskFile.getUploadUser(),diskFile.getIspublic(),  diskFile.getGroupName(),
				diskFile.getDownloadPwd() };
		this.jdbcTemplate.update(sql, params);
	}

	/**
	 * 删除文件
	 *
	 * @param sql
	 * @param params
	 */
	public void delDiskFile(String sql, Object[] params) {
		this.jdbcTemplate.update(sql, params);
	}

	/**
	 * 文件名模糊查询
	 *
	 * @param sql
	 * @param params
	 * @return
	 */
	public List<DiskFile> searchFile(String sql, Object[] params) {
		return findDiskFile(sql, params);
	}

	/**
	 * 查找文件列表
	 * 
	 * @param sql
	 * @param params
	 * @return
	 */
	public List<DiskFile> findDiskFileList(String sql, Object[] params) {

		diskFileList = new ArrayList<>();
		jdbcTemplate.query(sql, params, new RowCallbackHandler() {

			@Override
			public void processRow(ResultSet rs) throws SQLException {
				DiskFile diskFile = getDiskFile(rs);
				diskFileList.add(diskFile);
			}
		});
		return diskFileList;
	}

	/**
	 * 修改文件信息
	 *
	 * @param diskFile
	 */
	public void updateFile(DiskFile diskFile) {
		String sql = "update diskFile set fileName=?,fileFlag=?,ispublic=?,downloadNum=?,downloadPwd=? where UUID=?";
		Object[] params = { diskFile.getFileName(), diskFile.getFileFlag(), diskFile.getIspublic(),
				diskFile.getDownloadNum(), diskFile.getDownloadPwd(), diskFile.getUuid() };
		this.jdbcTemplate.update(sql, params);
	}

	/**
	 * 修改文件的状态
	 *
	 * @param sql
	 * @param params
	 */
	public void updateFileFlag(String sql, Object[] params) {
		this.jdbcTemplate.update(sql, params);
	}

	/**
	 * 获得用户上传文件总数
	 *
	 * @param sql
	 * @param params
	 * @return
	 */
	public Integer getFileCountByUserName(String sql, Object[] params) {
		System.out.println("========" + sql);
		if ("".equals(params)) {
			return jdbcTemplate.queryForObject(sql, Integer.class);
		} else {
			return jdbcTemplate.queryForObject(sql, params, Integer.class);
		}
	}


	/**
	 * 查询用户的全部上传文件
	 *
	 * @param userName
	 * @return
	 */
	public List<DiskFile> findDiskFileByUserName(String userName) {
		StringBuffer hql = new StringBuffer("select * from diskFile ");
		Object[] params = null;
		if (StringUtil.isNotEmpty(userName)) {
			hql.append(" where uploadUser like \"%\"?\"%\"");
			params = new Object[] { userName };
		}
		hql.append(" ORDER BY uploadDate desc ");
		return findDiskFile(hql.toString(), params);
	}

	/**
	 * 获得单条文件信息
	 *
	 * @param sql
	 * @param params
	 * @return 单条
	 */
	public DiskFile getDiskFile(String sql, Object[] params) {
		jdbcTemplate.query(sql, params, new RowCallbackHandler() {
			@Override
			public void processRow(ResultSet rs) throws SQLException {
				mDiskFile = getDiskFile(rs);
			}
		});
		return mDiskFile;
	}

	/**
	 * 返回文件信息
	 * 
	 * @param rs
	 * @return
	 */
	public DiskFile getDiskFile(ResultSet rs) {
		DiskFile diskFile = new DiskFile();
		try {
			diskFile.setDfsGroupName(rs.getString("dfsGroupName"));
			diskFile.setDownloadNum(rs.getInt("downloadNum"));
			diskFile.setDownloadPwd(rs.getString("downloadPwd"));
			diskFile.setExpirationDate(rs.getString("expirationDate"));
			diskFile.setExtra1(rs.getString("extra1"));
			diskFile.setExtra2(rs.getString("extra2"));
			diskFile.setExtra3(rs.getString("extra3"));
			diskFile.setExtra4(rs.getString("extra4"));
			diskFile.setExtra5(rs.getString("extra5"));
			diskFile.setFileExt(rs.getString("fileExt"));
			diskFile.setFileFlag(rs.getString("fileFlag"));
			diskFile.setFileName(rs.getString("fileName"));
			diskFile.setFileSize(rs.getLong("fileSize"));
			diskFile.setFileUrl(rs.getString("fileUrl"));
			diskFile.setGroupName(rs.getString("groupName"));
			diskFile.setIspublic(rs.getString("ispublic"));
			diskFile.setUploadDate(rs.getString("uploadDate"));
			diskFile.setUploadUser(rs.getString("uploadUser"));
			diskFile.setUuid(rs.getString("uuid"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return diskFile;
	}

	/**
	 * 获得文件列表信息
	 *
	 * @param sql
	 * @param params
	 * @return 集合
	 */
	public List<DiskFile> findDiskFile(String sql, Object[] params) {
		System.out.println("=====findDiskFile===" + sql);
		diskFileList = new ArrayList<>();
		if ("".equals(params)) {
			jdbcTemplate.query(sql, new RowCallbackHandler() {
				@Override
				public void processRow(ResultSet rs) throws SQLException {
					DiskFile diskFile = getDiskFile(rs);
					diskFileList.add(diskFile);
				}
			});
		} else {
			jdbcTemplate.query(sql, params, new RowCallbackHandler() {
				@Override
				public void processRow(ResultSet rs) throws SQLException {
					DiskFile diskFile = getDiskFile(rs);
					diskFileList.add(diskFile);
				}
			});
		}
		return diskFileList;
	}

	public DiskFileDao(JdbcTemplate jdbcTemplate) {
		super();
		this.jdbcTemplate = jdbcTemplate;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

}
